<?php
declare (strict_types=1);

namespace addons\V2\admin\controller;

use addons\Builder\facade\Builder;
use addons\V2\admin\model\ProductCate;
use app\common\model\Factory;
use think\facade\View;
use think\response\Json;

class Hypermarket extends \app\admin\controller\Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\addons\V2\admin\model\Product';

    //验证器
    protected string $validate = '\addons\V2\admin\validate\Product';

    //关联模型
    protected array $hasOne = ['cate1','factory'];

    public function index($extra_map = [['cate_1', '>=', 3]]): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '商品名称'])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'width' => 90, 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'cate_1', 'title' => '商品分区', 'templet' => '<div>{{d.cate1.title}}</div>'])
            ->addColumn(['field' => 'factory', 'title' => '厂家', 'templet' => '<div>{{d.factory.title}}</div>'])
            ->addColumn(['field' => 'price', 'title' => '市场价'])
            ->addColumn(['field' => 'vip_price', 'title' => '会员价'])
            ->addColumn(['field' => 'sales', 'title' => '销量'])
            ->addColumn(['field' => 'stock', 'title' => '库存'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'width' => 120, 'templet' => '#sort_sort'])
            ->addColumn(['field' => 'content', 'title' => '详情', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
//            ->addColumn(['field' => 'pick', 'title' => '推荐', 'templet' => '#pick', 'width' => 120])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['size' => '70%'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '70%'], 'delete'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭'],
                ['name' => 'pick', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->addSortTool([['name' => 'sort', 'pk' => 'id', 'url' => url('update')->build()]])
            ->addExtraMap($extra_map)
            ->addSearch([
                ['title', '输入商品名称/描述', 'input', '', '', '', 'like'],
                ['cate_1,cate_2', '一级分类', 'linkage', url('productCate/getCate')->build()]
            ])
            ->fetch();
    }


    public function add($tpl = '')
    {

        View::assign('cate', ProductCate::where(['pid' => 0,'delete_time'=>0])->where('id','>',2)->field('id,title')->select());
        View::assign('factory', Factory::where(['delete_time'=>0])->field('id,title')->select());
        return parent::add($this->app->getRootPath() . 'addons/V2/admin/view/hypermarket/edit.html'); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        View::assign('cate', ProductCate::where(['pid' => 0,'delete_time'=>0])->where('id','>',2)->field('id,title')->select());
        View::assign('factory', Factory::where(['delete_time'=>0])->field('id,title')->select());
        return parent::edit($id, $this->app->getRootPath() . 'addons/V2/admin/view/hypermarket/edit.html', $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        $param = $this->request->post();
        if (isset($param['images']) && $param['images']) {
            $param['icon'] = explode(',', $param['images'])[0];
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

}
